
<!DOCTYPE HTML>
<html lang="en" >
    <head>
        <title>Getting Started · GitBook</title>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.0">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-advanced-emoji/emoji-website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="installation.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="installation.html">
            
                <a href="installation.html">
            
                    
                    Installation
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3" data-path="started.html">
            
                <a href="started.html">
            
                    
                    Getting Started
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >Getting Started</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="getting-started">Getting Started</h1><h2 id="basic-usage">Basic Usage</h2><p>As the basic usage of validation, you can use the target element with <code>validity</code> wrap component, as follows:</p><h3 id="html">HTML</h3><pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">&quot;username&quot;</span>&gt;</span>username:<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">validity</span> <span class="hljs-attr">field</span>=<span class="hljs-string">&quot;username&quot;</span> <span class="hljs-attr">:validators</span>=<span class="hljs-string">&quot;{ required: true, minlength: 4 }&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> @<span class="hljs-attr">input</span>=<span class="hljs-string">&quot;handleValidate&quot;</span> @<span class="hljs-attr">focusout</span>=<span class="hljs-string">&quot;handleValidate&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">validity</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;errors&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;required&quot;</span> <span class="hljs-attr">v-if</span>=<span class="hljs-string">&quot;result.required&quot;</span>&gt;</span>required username!!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;minlength&quot;</span> <span class="hljs-attr">v-if</span>=<span class="hljs-string">&quot;result.minlength&quot;</span>&gt;</span>too short username!!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></code></pre>
<h3 id="javascript">JavaScript</h3><pre><code class="lang-javascript"><span class="hljs-keyword">new</span> Vue({
  data: {
    result: {}
  },
  methods: {
    handleValidate: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">e</span>) </span>{
      <span class="hljs-keyword">var</span> self = <span class="hljs-keyword">this</span>
      <span class="hljs-comment">// refer validity instance from target element</span>
      <span class="hljs-keyword">var</span> $validity = e.target.$validity 
      $validity.validate(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        self.result = $validity.result
      })
    }
  }
}).$mount(<span class="hljs-string">&apos;#app&apos;</span>)</code></pre>
<p>The validation results can get the <code>result</code> property from <code>validity</code> object. In above case, the <code>validity</code> object refer from <code>target</code> element of event, and run the validation with  call <code>validate</code> method of <code>validity</code> object, then validity result  refer from <code>result</code> property of <code>validity</code> object. This is a little similar the validation how to HTML5 form validation (Constraint validation API).</p><blockquote><p><img align="absmiddle" alt=":pencil:" class="emoji" src="gitbook/gitbook-plugin-advanced-emoji/emojis/pencil.png" title=":pencil:"> NOTE: You can also refer <code>validity</code> object with using <code>ref</code> special attribute on <code>validity</code> wrap component.</p></blockquote><h2 id="more-convenience-usage">More Convenience Usage</h2><p>As the more convenience usage of validation, you can wrap the <code>validity</code> wrap component with <code>validation</code> wrap component, as follows:</p><h3 id="html">HTML</h3><pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span>
  <span class="hljs-comment">&lt;!-- wrap validity component --&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">validation</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;validation1&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;username&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">&quot;username&quot;</span>&gt;</span>username:<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
      <span class="hljs-comment">&lt;!-- validity component wrapped with it --&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">validity</span> <span class="hljs-attr">field</span>=<span class="hljs-string">&quot;username&quot;</span> <span class="hljs-attr">:validators</span>=<span class="hljs-string">&quot;[&apos;required&apos;]&quot;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;username&quot;</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> @<span class="hljs-attr">input</span>=<span class="hljs-string">&quot;handleValidate&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">validity</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;password&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">&quot;password&quot;</span>&gt;</span>password:<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
      <span class="hljs-comment">&lt;!-- validity component wrapped with it --&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">validity</span> <span class="hljs-attr">field</span>=<span class="hljs-string">&quot;password&quot;</span> <span class="hljs-attr">:validators</span>=<span class="hljs-string">&quot;{ minlength: 8, required: true }&quot;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;password&quot;</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;password&quot;</span> @<span class="hljs-attr">input</span>=<span class="hljs-string">&quot;handleValidate&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">validity</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;confirm&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">&quot;confimr&quot;</span>&gt;</span>password (confirm):<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
      <span class="hljs-comment">&lt;!-- validity component wrapped with it --&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">validity</span> <span class="hljs-attr">field</span>=<span class="hljs-string">&quot;confirm&quot;</span> <span class="hljs-attr">:validators</span>=<span class="hljs-string">&quot;{ minlength: 8, required: true }&quot;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;confirm&quot;</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;password&quot;</span> @<span class="hljs-attr">input</span>=<span class="hljs-string">&quot;handleValidate&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">validity</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;send&quot;</span> <span class="hljs-attr">v-if</span>=<span class="hljs-string">&quot;valid&quot;</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;errors&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;username-invalid&quot;</span> <span class="hljs-attr">v-if</span>=<span class="hljs-string">&quot;usernameInvalid&quot;</span>&gt;</span>Invalid yourname inputting !!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;password-invalid&quot;</span> <span class="hljs-attr">v-if</span>=<span class="hljs-string">&quot;passwordInvalid&quot;</span>&gt;</span>Invalid password inputting !!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;confirm-invalid&quot;</span> <span class="hljs-attr">v-if</span>=<span class="hljs-string">&quot;confirmInvalid&quot;</span>&gt;</span>Invalid confirm password inputting !!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">validation</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></code></pre>
<h3 id="javascript">JavaScript</h3><pre><code class="lang-javascript"><span class="hljs-keyword">new</span> Vue({
  computed: VueValidator.mapValidation({
    valid: <span class="hljs-string">&apos;$validation.validation1.valid&apos;</span>,
    usernameInvalid: <span class="hljs-string">&apos;$validation.validation1.username.invalid&apos;</span>,
    passwordInvalid: <span class="hljs-string">&apos;$validation.validation1.password.invalid&apos;</span>,
    confirmInvalid: <span class="hljs-string">&apos;$validation.validation1.confirm.invalid&apos;</span>
  }),
  methods: {
    handleValidate: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">e</span>) </span>{
      e.target.$validity.validate()
    }
  }
}).$mount(<span class="hljs-string">&apos;#app&apos;</span>)</code></pre>
<p>By the wrapping <code>validation</code> wrap component, validation results for each <code>validity</code> wrap component is stored to <code>$validation</code> property of Vue instance.</p><p>These validation results can refer with specified the key to <code>name</code> property of <code>validation</code> wrap component. However, In above case, since the key path becomes longer, we provide the helper called <code>mapValidation</code> function. This is a similar the validation how to previous <code>2.x</code> version.</p>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="installation.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: Installation">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Getting Started","level":"1.3","depth":1,"previous":{"title":"Installation","level":"1.2","depth":1,"path":"installation.md","ref":"installation.md","articles":[]},"dir":"ltr"},"config":{"plugins":["edit-link","github","advanced-emoji@0.2.1"],"root":"./","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"github":{"url":"https://github.com/vuejs/vue-validator/"},"livereload":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"advanced-emoji":{"embedEmojis":false},"sharing":{"facebook":false,"twitter":false,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"edit-link":{"label":"Edit This Page","base":"https://github.com/vuejs/vue-validator/tree/dev/gitbook"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"language":"en","links":{"sharing":{"facebook":false,"twitter":false}},"gitbook":"*"},"file":{"path":"started.md","mtime":"2016-12-29T17:58:41.000Z","type":"markdown"},"gitbook":{"version":"3.2.0","time":"2016-12-29T16:08:54.292Z"},"basePath":".","book":{"language":"en"}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

